package org.ttitfly.ttcore.util.web;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

/**
 * 从request取得数据工具类
 *
 * @author: mahaibo
 * @since: 2007-12-1
 */
public class RequestParamUtil {
	/**
	 * 获取字符串值
	 *
	 * @param request	  HttpServletRequest
	 * @param paramname	parameter name
	 * @param defaultvalue default value
	 * @return request param value
	 */
	public static String getParam(HttpServletRequest request, String paramname, String defaultvalue) {
		String value = request.getParameter(paramname);
		if (StringUtil.haveText(value)) {
			return value;
		}
		return defaultvalue;
	}

	/**
	 * 获得日期
	 *
	 * @param request	  HttpServletRequest
	 * @param paramname	parameter name
	 * @param format	   format
	 * @param defaultvalue default value
	 * @return request param value
	 */
	public static Date getDateParam(HttpServletRequest request, String paramname, String format, Date defaultvalue) {
		String value = request.getParameter(paramname);
		if (!StringUtil.haveText(value)) {
			return defaultvalue;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(value);
		} catch (Exception e) {
			return defaultvalue;
		}
	}

	/**
	 * 获得双精度参数
	 *
	 * @param request	  HttpServletRequest
	 * @param paramname	parameter name
	 * @param defaultvalue default value
	 * @return param value
	 */
	public static Double getDoubleParam(HttpServletRequest request, String paramname, Double defaultvalue) {
		String value = request.getParameter(paramname);
		if (!StringUtil.haveText(value)) {
			return defaultvalue;
		}
		try {
			return Double.parseDouble(value);
		} catch (Exception e) {
			return defaultvalue;
		}
	}
	
	public static BigDecimal getBigDecimalParam(HttpServletRequest request, String paramname, BigDecimal defaultvalue) {
		String value = request.getParameter(paramname);
		if (!StringUtil.haveText(value)) {
			return defaultvalue;
		}
		try {
			return new BigDecimal(value);
		} catch (Exception e) {
			return defaultvalue;
		}
	}

	/**
	 * 获得浮点数
	 *
	 * @param request	  HttpServletRequest
	 * @param paramname	parameter name
	 * @param defaultvalue default value
	 * @return request param value
	 */
	public static Float getFloatParam(HttpServletRequest request, String paramname, Float defaultvalue) {
		String value = request.getParameter(paramname);
		if (!StringUtil.haveText(value)) {
			return defaultvalue;
		}
		try {
			return Float.parseFloat(value);
		} catch (Exception e) {
			return defaultvalue;
		}
	}

	/**
	 * 获得整数
	 *
	 * @param request	  HttpServletRequest
	 * @param paramname	parameter name
	 * @param defaultvalue default value
	 * @return request param value
	 */
	public static Integer getIntParam(HttpServletRequest request, String paramname, Integer defaultvalue) {
		String value = request.getParameter(paramname);
		if (!StringUtil.haveText(value)) {
			return defaultvalue;
		}
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {
			return defaultvalue;
		}
	}

	/**
	 * 获得整数
	 *
	 * @param request	  HttpServletRequest
	 * @param paramname	parameter name
	 * @param defaultvalue default value
	 * @return request param value
	 */
	public static Long getLongParam(HttpServletRequest request, String paramname, Long defaultvalue) {
		String value = request.getParameter(paramname);
		if (!StringUtil.haveText(value)) {
			return defaultvalue;
		}
		try {
			return Long.parseLong(value);
		} catch (Exception e) {
			return defaultvalue;
		}
	}


	/**
	 * 获得布尔值
	 *
	 * @param request	  HttpServletRequest
	 * @param paramname	parameter name
	 * @param defaultvalue default value
	 * @return request param value
	 */
	public static boolean getBooleanParam(HttpServletRequest request, String paramname, boolean defaultvalue) {
		String value = request.getParameter(paramname);
		if (!StringUtil.haveText(value)) {
			return defaultvalue;
		}
		try {
			return Boolean.parseBoolean(value);
		} catch (Exception e) {
			return defaultvalue;
		}
	}
}
